package com.pengliu.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pengliu.common.bean.SysRole;
import com.pengliu.config.BaseController;
import com.pengliu.service.SysAuthService;
import com.pengliu.service.vo.Menu;
import com.pengliu.util.AjaxResult;
import com.pengliu.util.Lists;
import com.pengliu.util.Maps;
import com.pengliu.util.SysConfig;

@Controller
@RequestMapping("/common")
public class CommonController extends BaseController{
	
	@Value("${companyName}")
	private String companyName;		//公司名称
	
	@Value("${version}")
	private String version;			//版本号
	
	@Autowired
	private SysAuthService sysAuthService;
	
	@ResponseBody
	@RequestMapping("/getCommonInfo")
	public AjaxResult getCommonInfo() {
		
		Map<String,Object> resultMap = Maps.newConcurrentHashMap();
		
		resultMap.put("companyName", companyName);
		resultMap.put("version", version);
		return AjaxResult.getSuccessrResult(resultMap);
	}
	
	@ResponseBody
	@RequestMapping("/getMenus")
	public AjaxResult getMenus() {
		
		SysRole sysRole = getCurrentSysRole();
		
		List<Menu> listMenu = sysAuthService.getMenuAll();
		
		listMenu = treeMenuList(sysRole.getId(), listMenu, 0);
		
		return AjaxResult.getSuccessrResult(listMenu);
	}
	
	//菜单树形结构
    public List<Menu> treeMenuList( Integer sysRoleId , List<Menu> menuList, Integer parentId) {
    	List<Menu> childMenu = Lists.newArrayList();
    	for (Menu menu : menuList) {
    		Integer menuId = menu.getId();
    		Integer pid = menu.getPid();
    		menu.setHasQX(sysRoleId.intValue() == menu.getRoleId());
    		if (parentId.intValue() == pid.intValue() && menu.isHasQX()) {
				List<Menu> c_node = treeMenuList(sysRoleId , menuList, menuId);
    			menu.setSubMenu(c_node);
    			childMenu.add(menu);
    		}
		}
        return childMenu;
    }
}
